<template>
  <div>
    <i-dialog
      :title="dialogType === 'BatchAdd' ? '批量绑定' : '批量删除'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
      @open="outerOpen"
    >
      <div>
        <LoadFile v-model="uploadData" ref="loadFile" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import LoadFile from '@/components/LoadFile'
import { snCodeMultiBind, snCodeMultiUnbind } from '@/api/device'
export default {
  name: 'SNBatchUpload',
  components: { LoadFile },
  data() {
    return {
      uploadData: [],
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    dialogType: {
      type: String,
      default: 'BatchAdd',
    },
  },
  methods: {
    refreshTable() {
      this.$emit('refreshTable')
    },
    outerClose() {
      this.$emit('update:visible', false)
    },
    outerOpen() {},
    validElementDeviceIdLength(params) {
      for (var i = 0, len = params.length; i < len; i++) {
        if (params[i].length > 32) {
          this.$message('数据错误,请检查导入的数据!设备号输入错误')
          return false
        }
      }
      return true
    },

    validElement(params) {
      for (var i = 0; i < params.length; i++) {
        if (params[i].deviceNum.length > 32) {
          this.$message('数据错误,请检查导入的数据!设备号输入错误')
          return false
        }

        if (!params[i].deviceNum || !params[i].snCode) {
          this.$message('数据错误,请检查导入的数据!设备与SN编码，需对应！')
          return false
        }
      }
      return true
    },

    handleBatchAdd() {
      let list = []
      this.uploadData.forEach((item) => {
        let itemObj = {}
        item.DeviceID &&
          (itemObj.deviceNum =
            typeof item.DeviceID === 'string'
              ? item.DeviceID.trim()
              : item.DeviceID)
        item.SNID &&
          (itemObj.snCode =
            typeof item.SNID === 'string' ? item.SNID.trim() : item.SNID)
        list.push(itemObj)
      })

      if (!this.validElement(list)) {
        this.$refs.loadFile.onRemove()
        return
      }
      snCodeMultiBind({
        list: list,
      }).then(
        () => {
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },
    handleBatchDelete() {
      let deviceNums = []
      this.uploadData.forEach((item) => {
        deviceNums.push(
          item.DeviceID && typeof item.DeviceID === 'string'
            ? item.DeviceID.trim()
            : item.DeviceID
        )
      })
      if (!this.validElementDeviceIdLength(deviceNums)) {
        this.$refs.loadFile.onRemove()
        return
      }

      snCodeMultiUnbind({
        deviceNums: deviceNums,
      }).then(
        () => {
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },

    submit() {
      switch (this.dialogType) {
        case 'BatchAdd':
          this.handleBatchAdd()
          break
        case 'BatchDelete':
          this.handleBatchDelete()
          break
        default:
          break
      }
    },
  },
}
</script>
